package com.benefm.multipar.pad.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

public class CircularProgressView extends View {
    private Paint mBackgroundPaint;
    private Paint mProgressPaint;
    private Paint mGlowPaint;

    private RectF mRectF;

    private float mProgress = 0f;
    private float mStrokeWidth = 20f;
    private int mProgressColor = Color.parseColor("#00FF00");
    private int mBackgroundColor = Color.GRAY;

    private ValueAnimator mProgressAnimator;

    public CircularProgressView(Context context) {
        super(context);
        init();
    }

    public CircularProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircularProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 背景画笔（黑色圆环）
        mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBackgroundPaint.setColor(mBackgroundColor);
        mBackgroundPaint.setStyle(Paint.Style.STROKE);
        mBackgroundPaint.setStrokeWidth(mStrokeWidth);
        mBackgroundPaint.setStrokeCap(Paint.Cap.ROUND);

        // 进度画笔（亮绿色）
        mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mProgressPaint.setColor(mProgressColor);
        mProgressPaint.setStyle(Paint.Style.STROKE);
        mProgressPaint.setStrokeWidth(mStrokeWidth);
        mProgressPaint.setStrokeCap(Paint.Cap.ROUND);

        // 发光点画笔
        mGlowPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mGlowPaint.setColor(mProgressColor);
        mGlowPaint.setStyle(Paint.Style.FILL);

        mRectF = new RectF();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float centerX = getWidth() / 2f;
        float centerY = getHeight() / 2f;
        float radius = Math.min(getWidth(), getHeight()) / 2f - mStrokeWidth / 2f;

        // 绘制背景圆环
        mRectF.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
        canvas.drawArc(mRectF, 0, 360, false, mBackgroundPaint);

        // 绘制进度圆环
        float sweepAngle = 360 * mProgress;
        canvas.drawArc(mRectF, -90, sweepAngle, false, mProgressPaint);

        // 绘制发光点
        float angle = -90 + sweepAngle;
        float glowX = centerX + (float) (radius * Math.cos(Math.toRadians(angle)));
        float glowY = centerY + (float) (radius * Math.sin(Math.toRadians(angle)));

        // 主发光点
        canvas.drawCircle(glowX, glowY, mStrokeWidth / 3f, mGlowPaint);

        // 随机分布的小发光点
        int numGlowPoints = 10;
        for (int i = 0; i < numGlowPoints; i++) {
            float pointAngle = (float) (Math.random() * sweepAngle - 90);
            float pointRadius = (float) (radius - Math.random() * mStrokeWidth / 2f);
            float pointX = centerX + (float) (pointRadius * Math.cos(Math.toRadians(pointAngle)));
            float pointY = centerY + (float) (pointRadius * Math.sin(Math.toRadians(pointAngle)));
            float pointSize = (float) (mStrokeWidth / 6f * Math.random());
            canvas.drawCircle(pointX, pointY, pointSize, mGlowPaint);
        }
    }

    public void setProgress(float progress) {
        mProgress = Math.max(0f, Math.min(1f, progress));
        invalidate();
    }

    public void setProgressWithAnimation(float progress, long duration) {
        if (mProgressAnimator != null && mProgressAnimator.isRunning()) {
            mProgressAnimator.cancel();
        }

        mProgressAnimator = ValueAnimator.ofFloat(mProgress, progress);
        mProgressAnimator.setDuration(duration);
        mProgressAnimator.setInterpolator(new LinearInterpolator());
        mProgressAnimator.addUpdateListener(animation -> {
            setProgress((float) animation.getAnimatedValue());
        });
        mProgressAnimator.start();
    }

    public void setStrokeWidth(float width) {
        mStrokeWidth = width;
        mBackgroundPaint.setStrokeWidth(width);
        mProgressPaint.setStrokeWidth(width);
        invalidate();
    }

    public void setProgressColor(int color) {
        mProgressColor = color;
        mProgressPaint.setColor(color);
        mGlowPaint.setColor(color);
        invalidate();
    }

    public void setBackgroundColor(int color) {
        mBackgroundColor = color;
        mBackgroundPaint.setColor(color);
        invalidate();
    }
}
